const express = require('express')
const bodyParser = require('body-parser');
const io = require('socket.io')(3000)

const app = express()
app.use(bodyParser())
app.get('/',function (req,res){
    res.status(200)
    res.send({player:'nakura'})
})

app.post('/leis',(req,res) => {
    console.log('req.body: ',req.body)
    res.send({message:'你好啊～'})
})

const server = app.listen(8081,'localhost', function () {

    const host = server.address().address
    const port = server.address().port

    console.log("应用实例，访问地址为 http://%s:%s", host, port)

})

io.on('connection', function (socket) {
    // 全部通知
    socket.emit('connect-check',{
        status:200
    })

    const emitCommon = (data = {}) => {
        socket.broadcast.emit('common-message', data);
        socket.emit('common-message', data);
    }
    const emitSysMessage = data => {
        socket.broadcast.emit('sys-message', data);
    }
    // 接收客户端来的消息
    socket.on('common-chat', function (data) {
        console.log(data.id,'===>',data.content.message)
        emitCommon(data)
    });
    // 接收客户端来的注册消息
    socket.on('common-reg', function (data) {
        emitSysMessage({
            id:data.id,
            type:'reg',
            status:200
        })
    });
});